package com.example.meitao.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.meitao.R;
import com.example.meitao.adapter.DetailAdapter;
import com.example.meitao.bean.Detail;
import com.example.meitao.config.NetConfig;
import com.example.meitao.task.DetailNetTask;

import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends AppCompatActivity {
    private ListView mListView;
    private TextView mTextViewTitle,mTextViewDui,mTextViewNei,mTextViewZan,mTextViewLai;
    private List<Detail.Databean.Subentitybean> data;
    private BaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {

    }

    private void setData() {
        adapter = new DetailAdapter(data,this);
        mListView.setAdapter(adapter);
    }

    private void initData() {
        data = new ArrayList<>();
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 1);
        new DetailNetTask(){
            @Override
            public void onCallBack(Detail result) {
                mTextViewTitle.setText(result.getData().getEname());
                mTextViewZan.setText(result.getData().getLikes()+"");
                mTextViewDui.setText("满370元免境内运费");
                mTextViewNei.setText(result.getData().getEditorcomment().getComments());
                mTextViewLai.setText("该商品来自"+result.getData().getOrigin_name());
                mTextViewTitle.setText(result.getData().getEname());
            }

        }.execute(NetConfig.ID_PATH);
        new DetailNetTask(){
            @Override
            public void onCallBack(Detail result) {
                data.addAll(result.getData().getSubentity());
                adapter.notifyDataSetChanged();
            }

        }.execute(NetConfig.ID_PATH);
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_detail_show);
        View headView = View.inflate(DetailActivity.this,R.layout.detail_head,null);
        mTextViewDui = (TextView) headView.findViewById(R.id.tv_item_discount_dui);
        mTextViewLai = (TextView) headView.findViewById(R.id.tv_item_discount_laizi);
        mTextViewNei = (TextView) headView.findViewById(R.id.tv_item_discount_nei);
        mTextViewTitle = (TextView) headView.findViewById(R.id.tv_detial_item_title);
        mTextViewZan = (TextView) headView.findViewById(R.id.tv_detial_item_zan);
        mListView.addHeaderView(headView);
    }
}
